package cn.neusoft.service.impl;

import cn.neusoft.mapper.AddressMapper;
import cn.neusoft.pojo.Address;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.AddressQuery;
import cn.neusoft.service.IAddressService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AddressService implements IAddressService {

    @Autowired
    AddressMapper addressMapper;

    @Transactional
    @Override
    public ResultBean add(Address address) {

        if (address==null ||
                StringUtils.isEmpty(address.getContactName()) ||
                StringUtils.isEmpty(address.getContactTel()) ||
                StringUtils.isEmpty(address.getAddress()) ||
                address.getUserId()==null || address.getUserId()==0){
            return ResultBean.ERROR("请求参数为空值");
        }

        int add = addressMapper.add(address);

        return add>0?ResultBean.SUCCESS(address.getAddressId()):ResultBean.ERROR("添加地址信息失败");
    }

    @Transactional
    @Override
    public ResultBean update(Address address) {

        if (address==null ||
                address.getAddressId()==null || address.getAddressId()==0 ||
                StringUtils.isEmpty(address.getContactName()) ||
                StringUtils.isEmpty(address.getContactTel()) ||
                StringUtils.isEmpty(address.getAddress()) ||
                address.getUserId()==null || address.getUserId()==0){
            return ResultBean.ERROR("请求参数为空值");
        }

        int update = addressMapper.update(address);

        return update>0?ResultBean.SUCCESS():ResultBean.ERROR("修改地址信息失败");
    }

    @Transactional
    @Override
    public ResultBean delete(Integer id) {

        if (id==null){
            return ResultBean.ERROR("请求参数为空值");
        }

        int delete = addressMapper.delete(id);

        return delete>0?ResultBean.SUCCESS():ResultBean.ERROR("删除地址信息失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getById(Integer id) {

        if (id==null){
            return ResultBean.ERROR("请求参数为空值");
        }

        Address address = addressMapper.getById(id);

        return address!=null?ResultBean.SUCCESS(address):ResultBean.ERROR("查询地址信息失败");
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getAll() {
        List<Address> addressList = addressMapper.getAll();
        return ResultBean.SUCCESS(addressList);
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getByPage(BaseQuery baseQuery) {

        System.out.println("条件对象："+baseQuery);

        if (baseQuery == null){
            baseQuery = new AddressQuery();
        }

        PageBean<Address> pageBean = new PageBean<>();
        int count = addressMapper.getCount(baseQuery);
        pageBean.setTotalRows(count);

        List<Address> addressList = addressMapper.getByPage(baseQuery);
        pageBean.setData(addressList);


        return ResultBean.SUCCESS(pageBean);
    }

    @Transactional
    @Override
    public ResultBean batchDel(List<Integer> ids) {

        if (ids==null || ids.size()==0){
            return ResultBean.ERROR("请求参数为空值");
        }

        for (Integer id:ids){
            int delete = addressMapper.delete(id);
            if (delete==0){
                throw new RuntimeException("删除失败");
            }
        }

        return ResultBean.SUCCESS();
    }

    @Transactional(readOnly = true)
    @Override
    public ResultBean getByUserId(Integer userId) {

        if (userId==null || userId==0){
            return ResultBean.ERROR("请求参数为空");
        }

        List<Address> addressList = addressMapper.getByUserId(userId);

        return ResultBean.SUCCESS(addressList);
    }
}
